﻿#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

int globalVar = 1;
static int staticGlobalVar = 1;
void Test()
{
	static int staticVar = 1;
	int localVar = 1;
	int num1[10] = { 1, 2, 3, 4 };
	char char2[] = "abcd";
	const char* pChar3 = "abcd";
	int* ptr1 = (int*)malloc(sizeof(int) * 4);
	int* ptr2 = (int*)calloc(4, sizeof(int));
	int* ptr3 = (int*)realloc(ptr2, sizeof(int) * 4);
	free(ptr1);
	free(ptr3);
}

#if 0

/*
operator new：该函数实际通过malloc来申请空间，当malloc申请空间成功时直接返回；申请空间
失败，尝试执行空间不足应对措施，如果改应对措施用户设置了，则继续申请，否则抛异常。
*/
void* __CRTDECL operator new(size_t size) _THROW1(_STD bad_alloc)
{
	// try to allocate size bytes
	void* p;
	while ((p = malloc(size)) == 0)
		if (_callnewh(size) == 0)
		{
			// report no memory
			// 如果申请内存失败了，这里会抛出bad_alloc 类型异常
			static const std::bad_alloc nomem;
			_RAISE(nomem);
		}
	return (p);
}
/*
operator delete: 该函数最终是通过free来释放空间的
*/
void operator delete(void* pUserData)
{
	_CrtMemBlockHeader* pHead;
	RTCCALLBACK(_RTC_Free_hook, (pUserData, 0));
	if (pUserData == NULL)
		return;
	_mlock(_HEAP_LOCK);  /* block other threads */
	__TRY
		        /* get a pointer to memory block header */
		pHead = pHdr(pUserData);
	         /* verify block type */
	_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));
	_free_dbg(pUserData, pHead->nBlockUse);
	__FINALLY
		_munlock(_HEAP_LOCK);  /* release other threads */
	__END_TRY_FINALLY
		return;
}
/*
free的实现
*/
#define   free(p)               _free_dbg(p, _NORMAL_BLOCK)//free其实是一个宏函数

#endif

class A
{
public:
	//不是默认构造函数（必须要传参）
	A(int a=0,int b=0)
		:_a(a)
		,_b(b)
	{
		cout << "调用构造" << endl;
	}
	~A()
	{
		cout << "调用析构" << endl;
	}
private:
	int _a=0;
	int _b = 0;

};

int main()
{
	A* p1 = (A*)malloc(sizeof(A));//只开辟了空间没有初始化
	new(p1)A;//手动初始化
	p1->~A();//手动析构
	free(p1);

	//operator new的底层就是malloc，所以用法与malloc一样，不过它们的区别就是内存开辟失败时一个直接返回空，一个抛异常
	A* p2 = (A*)operator new(sizeof(A));
	new(p2)A(1,2);//手动初始化,如果没有默认构造就必须要传参！
	p2->~A();
	operator delete(p2);
	return 0;
}
int main8()
{
	A* p1 = new A[3];
	delete[] p1;
	return 0;
}

int main7()
{
	A* p1 = new A;
	delete p1;
	return 0;
}



void func()
{
	int n = 0;
	while (1)
	{
		n++;
		void* p = new char[1024 * 1024];//一次向堆申请一字节的内存
		cout << p << ":->" << n << endl;
	}
}

int main6()
{
	try
	{
		func();
	}
	catch (const exception& e)
	{
		cout << e.what() << endl;
	}
	return 0;
}
int main5()
{
	try
	{
		void* p1 = new char[1024 * 1024 * 1024];//向堆申请了1G的空间
		void* p2 = new char[1024 * 1024 * 1024];
		void* p3 = new char[1024 * 1024 * 1024];
		void* p4 = new char[1024 * 1024 * 1024];
		void* p5 = new char[1024 * 1024 * 1024];

	}
	catch (const exception& e)
	{
		cout<<e.what()<<endl;
	}
	return 0;
}
int main4()
{
	A* p1 = new A(1);
	A* p2 = new A(1,2);

	A aa1(1, 1);
	A aa2(2, 2);
	A aa3(3, 3);
	//A* p3 = new A[3];//编译报错:A类不存在默认构造函数
	A* p3 = new A[3]{ aa1,aa2,aa3 };//直接给成员
	A* p4 = new A[3]{ A(1,1),A(2,2),A(3,3)};//创建匿对象
	A* p5 = new A[3]{ {1,1 },{2,2},{3,3,}};//隐式类型转换创建对象
	return 0;
}

struct ListNode
{
	int val;
	ListNode* next;
	ListNode(int x=0)
		:val(x)
		,next(nullptr)
	{}
};

int main3()
{
	ListNode* n1 = new ListNode(1);
	ListNode* n2 = new ListNode(2);
	ListNode* n3 = new ListNode(3);
	ListNode* n4 = new ListNode(4);
	ListNode* n5 = new ListNode(5);
	n1->next = n2;
	n2->next = n3;
	n3->next = n4;
	n4->next = n5;
	delete n1;
	delete n2;
	delete n3;
	delete n4;
	delete n5;
	return 0;
}

#if 0
int main2()
{
	//只会在堆上开辟空间
	A* p1 = (A*)malloc(sizeof(A));
	//不仅会开辟空间，还会调用对象的构造函数进行初始化
	A* p2 = new A(1);
	
	//只会释放空间
	free(p1);
	p1 = nullptr;
	//回先调用对象的析构函数释放资源，再释放为对象开辟的空间
	delete p2;

	// 内置类型是几乎是一样的
	int* p3 = (int*)malloc(sizeof(int)); // C
	int* p4 = new int;
	free(p3);
	delete p4;
	A* p5 = (A*)malloc(sizeof(A) * 10);
	A* p6 = new A[10];
	free(p5);
	delete[] p6;
	return 0;
}

int main1()
{
	//动态申请一个int类型的空间
	int* ptr1 = new int;
	//动态申请一个int类型的空间并初始化为10
	int* ptr2 = new int(10);
	//动态申请10个int类型的空间
	int* ptr3 = new int[10];
	//动态申请10个int类型的空间并全部初始化为0
	int* ptr4 = new int[10] {0};
	//动态申请10个int类型的空间并将前五个初始化，后面的默认为0
	int* ptr5 = new int[10] {1,2,3,4,5};

	//匹配起来使用
	delete ptr1;
	delete ptr2;
	delete[] ptr3;
	delete[] ptr4;
	delete[] ptr5;
	return 0;
}
#endif